package cn.xenosp.server.common.message.email;

import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;

import java.util.List;

/**
 * 项目名： service-web
 * 包路径： cn.xenosp.server.common.message.email
 * 作者：   TongHui
 * 创建时间: 2025-01-21 14:23
 * 描述: TODO
 * 版本: 1.0
 */
public class SendEmailMessage {

    /**
     *
     * @param message 消息实体
     * @param toMail  收件人
     * @return
     */
    public String send(MailMessage message, List<String> toMail){
        MailAccount mailAccount = getMailAccount(message);
        return MailUtil.send(mailAccount, toMail, message.getTitle(), message.getContent(), true);
    }

    public String send(MailMessage message, String toMail){
        MailAccount mailAccount = getMailAccount(message);
        return MailUtil.send(mailAccount, toMail, message.getTitle(), message.getContent(), true);
    }

    private MailAccount getMailAccount(MailMessage message){
        MailAccount mailAccount = new MailAccount();
        mailAccount.setHost(message.getHost());
        mailAccount.setPort(message.getPort());
        mailAccount.setAuth(true);
        mailAccount.setFrom(message.getMail());
        mailAccount.setUser(message.getUsername());
        mailAccount.setPass(message.getPassword());
        mailAccount.setSslEnable(message.getSslEnable());
        mailAccount.setStarttlsEnable(message.getStarttlsEnable());
        return mailAccount;
    }
}
